
//
//  YDAliPayViewController.m
//  PeiZiWangBei
//
//  Created by 巨彦新 on 2018/1/18.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "YDAliPayViewController.h"
#import "YDRechargeTopView.h"
#import "YDRechargeWithdrawalButtonView.h"
#import "OttoKeyboardView.h" //自定义键盘

@interface YDAliPayViewController ()<UITextFieldDelegate,SYRechargeWithdrawalButtonViewDelegate,UIWebViewDelegate,TextFieldClickReturnDelegate>
//顶部显示余额
@property (nonatomic , weak) YDRechargeTopView *topView;
//账号
@property (nonatomic , weak) UIView *accountContentView;
//账号label
@property (nonatomic , weak) UILabel *accountLabel;
//账号输入框
@property (nonatomic , weak) UITextField *accountTextField;

//金额
@property (nonatomic , weak) UIView *moenyContentView;
//金额label
@property (nonatomic , weak) UILabel *moenyLabel;
//金额输入框
@property (nonatomic, strong) OttoTextField *moneyTextField;
//温馨提示webview
@property (nonatomic, strong)UIWebView *tipsWebview;



//充值按钮
@property (nonatomic , weak) YDRechargeWithdrawalButtonView *buttonView;
@end

@implementation YDAliPayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"支付宝转账";
    [self.navigationController.navigationBar setHidden:YES];
    [self dismissKeyBoard];
    [self topView];
    [self addTextField];
    [self tipsWebview];
    [self buttonView];
    
}

#pragma mark - 点击充值
- (void)rechargeWithdrawalButtonViewButtonClick
{
    ///金额限制2元
    if(![NSString isFloatNumString:self.moneyTextField.text])
    {
        [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"请输入正确的金额，金额不能低于100元" isExistNav:NO];
        return;
    }
    ///金额限制1个亿
    if([self.moneyTextField.text doubleValue] > 100000000)
    {
        [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"金额多大，请重新输入" isExistNav:NO];
        return;
    }
    
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:@{
                                                                                 @"alipayAccount":self.accountTextField.text,
                                                                                 @"amount":self.moneyTextField.text
                                                                                 }];
    [[SYProgressHUD sharedInstance] showMBHudWithSting:kLoadingText];
    [BaseNetRequest sessionRequestWithparameters:dic
                                               method:POSTMethod
                                           requstType:AlipayForwrad
                                     WithSuccessBlock:^(NSDictionary *resultDict) {
                                         [[SYProgressHUD sharedInstance] dismissMBWithTime:0];
                                         if ([resultDict[@"code"] integerValue] == 200) {
                                             
                                             [[UIApplication sharedApplication] openURL:[NSURL URLWithString:resultDict[@"result"][@"forward"]]];
                                         }else
                                         {
                                             [[NavTipsView defaultTipsView] showTipsBannerInView:self.view
                                                                                             WithType:NavTipsViewTypeMessage
                                                                                                 text:resultDict[@"message"]
                                                                                           isExistNav:YES];
                                         }
                                         
                                     } failBlock:^(NSError *error) {
                                         [[SYProgressHUD sharedInstance] dismissMBWithTime:0];
                                         [[NavTipsView defaultTipsView] showTipsBannerInView:self.view
                                                                                         WithType:NavTipsViewTypeMessage
                                                                                             text:@"支付宝转账失败"
                                                                                       isExistNav:YES];
                                     }];

}


//添加输入框
-(void)addTextField
{
    [self accountContentView];
    [self accountLabel];
    [self accountTextField];
    
    [self moenyContentView];
    [self moenyLabel];
    [self moneyTextField];
    
}

#pragma mark -UITextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.view endEditing:YES];
    return YES;
}
- (void)textFieldDidChange:(UITextField *)textField
{
    if (self.accountTextField.text.length && self.moneyTextField.text.length) {
        [self.buttonView enableButton];
    }else
    {
        [self.buttonView disableButton];
    }
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSArray *subviews = [self.view subviews];
    for (UITextField *textField in subviews) {
        if (textField == self.moneyTextField) {
            [self.moneyTextField resignFirstResponder];
        }
    }
    
}
#pragma mark - TextViewClickReturnDelegate
- (void)textViewClickReturn:(OttoTextView *)textView{
    [textView resignFirstResponder];
    NSLog(@"return - %@",textView.text);
}

#pragma mark - TextFieldClickReturnDelegate
- (void)textFieldClickReturn:(OttoTextField *)textField{
    [textField resignFirstResponder];
    NSLog(@"return - %@",textField.text);
}
////*******YDRechargeTopView********////
-(YDRechargeTopView *)topView
{
    if (_topView == nil) {
        NSString *accountString = [[SYUserDataManger sharedInstance] getUserAccountNumberString];
        YDRechargeTopView *topView = [YDRechargeTopView rechargeTopViewWithAmount:accountString];
        __weak typeof(self)weself = self;
        topView.title = @"支付宝转账";
        [topView setBackBlock:^(BOOL isSelected) {
            [weself.navigationController popViewControllerAnimated:YES];
        }];
        [self.view addSubview:topView];
        [topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self.view);
            if (isPhoneX) {
                make.height.equalTo(@(rateX812(170) + 24));
            }else{
                make.height.equalTo(@(rate750(170)));
            }
        }];
        _topView = topView;
    }
    return _topView;
}
///*********accountContentView*******///////
-(UIView *)accountContentView
{
    if (_accountContentView == nil) {
        UIView *accountContentView = [[UIView alloc] init];
        [self.view addSubview:accountContentView];
        accountContentView.backgroundColor = [UIColor whiteColor];
        accountContentView.userInteractionEnabled = YES;
        [accountContentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            if (isPhoneX) {
                make.top.equalTo(self.topView.mas_bottom).offset(rateX812(10));
                make.height.equalTo(@(rateX812(50)));
            }else{
                make.top.equalTo(self.topView.mas_bottom).offset(rate750(10));
                make.height.equalTo(@(rate750(50)));
            }
        }];
        _accountContentView = accountContentView;
    }
    return _accountContentView;
}
//账号label
-(UILabel *)accountLabel
{
    if (_accountLabel == nil) {
        UILabel *accountLabel = [[UILabel alloc] init];
        [self.accountContentView addSubview:accountLabel];//104 114 132
        accountLabel.textColor = NumberColor(104, 114, 132);
        accountLabel.font = [UIFont systemFontOfSize:font14ScreenWRate * LKScreenW];
        accountLabel.text = @"账号";
        accountLabel.textAlignment = NSTextAlignmentCenter;
        [accountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.accountContentView);
            make.left.equalTo(self.accountContentView);
            make.width.equalTo(@(rate375(61)));
        }];
        _accountLabel = accountLabel;
    }
    return _accountLabel;
}
-(UITextField *)accountTextField
{
    if (_accountTextField == nil) {
        UITextField *accountTextField = [[UITextField alloc] init];
        [self.accountContentView addSubview:accountTextField];
        accountTextField.textColor = [UIColor blackColor];
        accountTextField.font = [UIFont systemFontOfSize:font14ScreenWRate * LKScreenW];
        accountTextField.placeholder = @"请输入自己的支付宝账号";
        accountTextField.backgroundColor = [UIColor clearColor];
        accountTextField.borderStyle = UITextBorderStyleNone;
        accountTextField.textAlignment = NSTextAlignmentLeft;
//        accountTextField.keyboardType = UIKeyboardTypeNumberPad;
        [accountTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        accountTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        
        accountTextField.returnKeyType =UIReturnKeyDone;
        [accountTextField setClearButtonMode:UITextFieldViewModeWhileEditing];
        [accountTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.accountContentView);
            make.left.equalTo(self.accountLabel.mas_right);
            make.right.equalTo(self.accountContentView.mas_right).offset(-rate375(16));
            if (isPhoneX) {
                make.height.equalTo(@(rateX812(20)));
            }else{
                make.height.equalTo(@(rate750(20)));
            }
        }];
        accountTextField.tintColor = [UIColor blackColor];
        accountTextField.delegate = self;
        
        _accountTextField = accountTextField;
    }
    return _accountTextField;
}


/**
 moenyview
 */
-(UIView *)moenyContentView
{
    if (_moenyContentView == nil) {
        UIView *accountContentView = [[UIView alloc] init];
        [self.view addSubview:accountContentView];
        accountContentView.backgroundColor = [UIColor whiteColor];
        accountContentView.userInteractionEnabled = YES;
        [accountContentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            if (isPhoneX) {
                make.top.equalTo(self.accountContentView.mas_bottom).offset(rateX812(1));
                make.height.equalTo(@(rateX812(50)));
            }else{
                make.top.equalTo(self.accountContentView.mas_bottom).offset(rate750(1));
                make.height.equalTo(@(rate750(50)));
            }
        }];
        _moenyContentView = accountContentView;
    }
    return _moenyContentView;
}
//金额label
-(UILabel *)moenyLabel
{
    if (_moenyLabel == nil) {
        UILabel *accountLabel = [[UILabel alloc] init];
        [self.moenyContentView addSubview:accountLabel];//104 114 132
        accountLabel.textColor = NumberColor(104, 114, 132);
        accountLabel.font = [UIFont systemFontOfSize:font14ScreenWRate * LKScreenW];
        accountLabel.text = @"金额";
        accountLabel.textAlignment = NSTextAlignmentCenter;
        [accountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.moenyContentView);
            make.left.equalTo(self.moenyContentView);
            make.width.equalTo(@(rate375(61)));
        }];
        _moenyLabel = accountLabel;
    }
    return _moenyLabel;
}
-(OttoTextField *)moneyTextField
{
    if (_moneyTextField == nil) {
        OttoTextField *accountTextField = [[OttoTextField alloc] init];
        [self.moenyContentView addSubview:accountTextField];
        accountTextField.textColor = [UIColor blackColor];
        accountTextField.font = [UIFont systemFontOfSize:font14ScreenWRate * LKScreenW];
        accountTextField.placeholder = @"请输入充值金额";
        accountTextField.backgroundColor = [UIColor clearColor];
        accountTextField.borderStyle = UITextBorderStyleNone;
        accountTextField.textAlignment = NSTextAlignmentLeft;
        accountTextField.keyboardType = UIKeyboardTypeNumberPad;
        [accountTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        
        [accountTextField setKeyboardType:KeyboardTypeNumber];
        [accountTextField setNumberKeyboardType:NumberKeyboardTypeNumber];
        accountTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        accountTextField.textFieldClickReturnDelegate = self;
        accountTextField.returnKeyType =UIReturnKeyDone;
        [accountTextField setClearButtonMode:UITextFieldViewModeWhileEditing];
        [accountTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.moenyContentView);
            make.left.equalTo(self.moenyLabel.mas_right);
            make.right.equalTo(self.moenyContentView.mas_right).offset(-rate375(16));
            if (isPhoneX) {
                make.height.equalTo(@(rateX812(20)));
            }else{
                make.height.equalTo(@(rate750(20)));
            }
        }];
        accountTextField.tintColor = [UIColor blackColor];
        accountTextField.delegate = self;
        
        _moneyTextField = accountTextField;
    }
    return _moneyTextField;
}
/////********充值按钮*******///////
-(YDRechargeWithdrawalButtonView *)buttonView
{
    if (_buttonView == nil) {
        YDRechargeWithdrawalButtonView *buttonView = [YDRechargeWithdrawalButtonView rechargeWithdrawalButtonWithButtonTitle:@"充值"];
        buttonView.delegate = self;
        [self.view addSubview:buttonView];
        
        [buttonView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            if (isPhoneX) {
                make.height.equalTo(@(rateX812(63)));
            }else{
                make.height.equalTo(@(rate750(50)));
            }
        }];
        _buttonView = buttonView;
    }
    return _buttonView;
}

- (void)callPhone
{
    NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",CustomerPhone];
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self callPhone];
        });
        return NO;
    }else
    {
        return YES;
    }
    
}
-(UIWebView *)tipsWebview
{
    if (_tipsWebview == nil) {
        _tipsWebview = [UIWebView new];
        _tipsWebview.scrollView.scrollEnabled = NO;
        _tipsWebview.delegate = self;
        _tipsWebview.userInteractionEnabled = YES;
        [self.view addSubview:_tipsWebview];
        _tipsWebview.backgroundColor = [UIColor colorWithHexString:mainGray];
        [_tipsWebview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            
            if (isPhoneX) {
                make.top.equalTo(self.moenyContentView).offset(rateX812(77));
            }else{
                make.top.equalTo(self.moenyContentView).offset(rate750(77));
            }
            make.height.equalTo(@200);
        }];
        [_tipsWebview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"WarmTips_AliPay" ofType:@".html" ]]]];
        
    }
    return _tipsWebview;
}
@end
